//
//  VipInfoMainViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/27.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipInfoMainViewController.h"
#import "VipInfoMainView.h"
#import "SMPagerTabView.h"
#import "VipInfo1ViewController.h"
#import "VipInfo2ViewController.h"
#import "VipInfo3ViewController.h"
#import "VipInfo4ViewController.h"
#import "SMPagerScrollView.h"
#import "PhoneAlertView.h"
#import "CZQVipDetalChildViewController.h"

@interface VipInfoMainViewController ()<UIScrollViewDelegate>

@property (nonatomic,weak) VipInfoMainView *vipInfoMainView;


/**
*  内容视图
*/
@property(nonatomic,weak)SMPagerScrollView *contentView;

@end

@implementation VipInfoMainViewController

-(void)viewWillAppear:(BOOL)animated{
    
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"会员详情";
    
    self.view.backgroundColor = CSClearColor;
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createSubView];
    
    // 初始化所有子控制器
    [self setupChildVces];
    
    // 设置顶部的标签栏
    //[self setupTitlesView];
    
    // 底部cententView
    [self setupContentView];
}

/**
 *  初始化所有子控制器
 */
- (void)setupChildVces
{
    /**
     * 基本信息
     **/
    CZQVipDetalChildViewController *vipInfoVC1 = [[CZQVipDetalChildViewController alloc] init];
    vipInfoVC1.childType = 1;
    vipInfoVC1.venueVipModel = self.venueVipModel;
    [self addChildViewController:vipInfoVC1];
    
    /**
     * 上课记录
     **/
    CZQVipDetalChildViewController *vipInfoVC2 = [[CZQVipDetalChildViewController alloc] init];
    vipInfoVC2.childType = 2;
    vipInfoVC2.venueVipModel = self.venueVipModel;
    [self addChildViewController:vipInfoVC2];
    
    /**
     * 跟进记录
     **/
    VipInfo3ViewController *vipInfoVC3 = [[VipInfo3ViewController alloc] init];
    vipInfoVC3.venueVipModel = self.venueVipModel;
    [self addChildViewController:vipInfoVC3];
    
    /**
     * 更多信息
     **/
    CZQVipDetalChildViewController *vipInfoVC4 = [[CZQVipDetalChildViewController alloc] init];
    vipInfoVC4.childType = 4;
    vipInfoVC4.venueVipModel = self.venueVipModel;
    [self addChildViewController:vipInfoVC4];
    
}

/**
 *  底部cententView
 */
- (void)setupContentView
{
    // 不要自动调整inset
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    SMPagerScrollView *contentView = [[SMPagerScrollView alloc] init];
    contentView.frame = CGRectMake(0, self.vipInfoMainView.height, self.view.width, SCREEN_HEIGHT - self.vipInfoMainView.height - adaptNavagationBar);
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [self.view addSubview:contentView];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, contentView.height);
    self.contentView = contentView;
    for (int i = 0; i < self.childViewControllers.count; i++) {
        
        // 取出子控制器
        UIViewController *vc = self.childViewControllers[i];
        
        vc.view.frame = CGRectMake(contentView.width * i, 0, contentView.width, contentView.height);
        
        [contentView addSubview:vc.view];
    }
    
    // 添加第一个控制器的view
    //[self scrollViewDidEndScrollingAnimation:contentView];
}

-(void)createSubView{
    
    //设置背景颜色
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_mbg"]forBarMetrics:UIBarMetricsDefault];
    
    VipInfoMainView *vipInfoMainView = [[VipInfoMainView alloc] initWithFrame:CGRectMake(0, -2, kScreenWidth, 170)];
    
//    VipInfoMainView *vipInfoMainView = [VipInfoMainView new];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",self.venueVipModel.Pic,mid_image];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [vipInfoMainView.headImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:PlaceHold];
    
    NSDateComponents *componet = [NSString getDateDifference:self.venueVipModel.Birthday];
    
//    //年龄
    vipInfoMainView.ageLabel.text = [NSString stringWithFormat:@"%ld岁",labs(componet.year)];
    
    //姓名
    vipInfoMainView.nickNameLabel.text = self.venueVipModel.Name;
    
    //性别
    vipInfoMainView.sexImage.image = [self.venueVipModel.Sex isEqualToString:@"0"]?[UIImage imageNamed:@"icon_man"]:[UIImage imageNamed:@"icon_women"];
    
    //手机
    vipInfoMainView.phoneLabel.text = self.venueVipModel.CellPhone;
    
    //后退
    [vipInfoMainView.backBtn addTarget:self action:@selector(backBtnClick)];
    
    //设置
    [vipInfoMainView.setBtn addTarget:self action:@selector(setBtnClick)];
    
    //更多信息
    [vipInfoMainView.infoBtn addTarget:self action:@selector(infoBtnClick)];
    
    //电话
    [vipInfoMainView.phoneBtn addTarget:self action:@selector(phoneBtnClick)];
    
    //信息
    [vipInfoMainView.msgBtn addTarget:self action:@selector(msgBtnClick)];
    
    [vipInfoMainView.btn1 addTarget:self action:@selector(btnClick:)];
    [vipInfoMainView.btn2 addTarget:self action:@selector(btnClick:)];
    [vipInfoMainView.btn3 addTarget:self action:@selector(btnClick:)];
    [vipInfoMainView.btn4 addTarget:self action:@selector(btnClick:)];
    
    [self.view addSubview:vipInfoMainView];
    
    self.vipInfoMainView = vipInfoMainView;
    
//    [vipInfoMainView mas_remakeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.left.width.mas_equalTo(self.view);
//
//    }];
    
    [vipInfoMainView setNeedsLayout];
    [vipInfoMainView layoutIfNeeded];
}

#pragma mark - <UIScrollViewDelegate>

/**
 *  在scrollview停止滑动的时候执行
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //[self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击菜单按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    if (index == 0) {
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn1.centerX;
    }
    else if (index == 1){
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn2.centerX;
    }
    else if (index == 2){
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn3.centerX;
    }
    else{
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn4.centerX;
    }
    
}

#pragma mark 按钮点击
-(void)btnClick:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    
    [self.contentView setContentOffset:CGPointMake(self.contentView.width * tag, 0) animated:YES];
    
    if (tag == 0) {
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn1.centerX;
    }
    else if (tag == 1){
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn2.centerX;
    }
    else if (tag == 2){
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn3.centerX;
    }
    else{
        
        self.vipInfoMainView.sanJiaoImageView.centerX = self.vipInfoMainView.btn4.centerX;
    }
}


//后退
-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

//设置
-(void)setBtnClick{
    
    /**
     * 更多信息
     **/
    VipInfo4ViewController *vipInfoVC4 = [[VipInfo4ViewController alloc] init];
    
    vipInfoVC4.venueVipModel = self.venueVipModel;
    
    [self addChildViewController:vipInfoVC4];
    
    [self.navigationController pushViewController:vipInfoVC4 animated:YES];
}

//更多信息
-(void)infoBtnClick{
    
    
}

//电话
-(void)phoneBtnClick{

    NSString *phone = self.venueVipModel.CellPhone;
    
    NSMutableString *str = [[NSMutableString alloc]initWithString:phone];//存在堆区
    
    [str insertString:@"-" atIndex:3];
    
    [str insertString:@"-" atIndex:7];
    
    PhoneAlertView *phoneAlertView = [[PhoneAlertView alloc] init];
    phoneAlertView.phone = phone;
    phoneAlertView.phoneLabel.text = str;
    phoneAlertView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [[UIApplication sharedApplication].keyWindow addSubview:phoneAlertView];
}

//信息
-(void)msgBtnClick{
    
    NSString *url = [NSString stringWithFormat:@"sms://%@",self.venueVipModel.CellPhone];
    
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle

{
    
    return UIStatusBarStyleLightContent;    
}
@end
